from rest_framework import serializers
from rest_framework.response import Response

from apps.headline.models import Article
from apps.question.models import Label, Question, Reply
from apps.recruit.models import Enterprise
from apps.users.models import User


class ArticesSerialzier(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'title']


class UserTwoSerializer(serializers.ModelSerializer):
    articles = ArticesSerialzier(read_only=True, many=True)
    fans = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = User
        fields = '__all__'  ########################################################################


#######################################################################################

class LabelSerializer(serializers.ModelSerializer):
    users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Label
        fields = ['id', 'label_name', 'users']


class QuestionsSreializer(serializers.ModelSerializer):
    labels = LabelSerializer(read_only=True, many=True)
    user = serializers.StringRelatedField()

    class Meta:
        model = Question
        fields = ['id', 'createtime', 'labels', 'reply', 'replyname', "replytime",
                  'title', 'unuseful_count', 'useful_count', "user", 'visits']


class SubsSerializer(serializers.ModelSerializer):
    user = UserTwoSerializer(read_only=True, many=True)

    class Meta:
        model = Reply
        fields = "__all__"


class AnswerSerializer(serializers.ModelSerializer):
    problem = serializers.PrimaryKeyRelatedField(read_only=True)
    subs = SubsSerializer(read_only=True, many=True)
    user = UserTwoSerializer(read_only=True)
    parent = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Reply
        fields = '__all__'


class CollectSerializer(serializers.ModelSerializer):
    user = UserTwoSerializer(read_only=True)
    collected_users = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Article
        fields = '__all__'


class EnterSerializer(serializers.ModelSerializer):
    class Meta:
        model = Enterprise
        fields = ['id', 'name', 'labels', 'logo', 'recruits', 'summary']


#####################################################################################
class Userserializer(serializers.ModelSerializer):
    labels = LabelSerializer(read_only=True, many=True)
    questions = QuestionsSreializer(read_only=True, many=True)
    answer_question = AnswerSerializer(read_only=True, many=True)
    collected_articles = CollectSerializer(read_only=True, many=True)
    enterpises = EnterSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ['id', 'username', 'mobile', 'realname', 'birthday', 'sex',
                  'avatar', 'email', 'city', 'address', 'labels', 'questions', 'answer_question',
                  'collected_articles', 'enterpises', ]

        extra_kwargs = {
            'username': {'read_only': True},
        }
